//
//  RealName.swift
//  LeGou
//
//  Created by lxm on 2022/8/3.
//

import UIKit

class RealName: ZMViewController {
    //MARK: --------------------属性--------------------
    
    //MARK: --------------------UI控件--------------------
    let zjlx = ZMUtil.label(title: "证件类型", color: UIColor.zm.color333333(), font: UIFont.boldSystemFont(ofSize: 14))
    
    let typeView = UIView().then { v in
        let sfz = ZMUtil.label(title: "身份证", color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 14))
        v.addSubview(sfz)
        v.layer.cornerRadius = 12
        v.backgroundColor = .white
        sfz.sizeToFit()
        sfz.zm.setY(12)
        sfz.zm.setX(12)
        sfz.zm.setCenterY(24)
    }
    
    let xm = ZMUtil.label(title: "姓名", color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 14))
    
    let nameContent = UIView().then({$0.backgroundColor = .white;$0.layer.cornerRadius = 12})

    let nameText = UITextField().then { t in
        t.font = UIFont.systemFont(ofSize: 14)
        t.MaxCharcterCount = 20
        t.attributedPlaceholder = "输入姓名".zm.attributedStr(attrs: [.foregroundColor : "#999999".zm.hex()])
    }
    
    let sfzh = ZMUtil.label(title: "身份证号", color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 14))
    
    let idcardContent = UIView().then({$0.backgroundColor = .white;$0.layer.cornerRadius = 12})

    let idcardText = UITextField().then { t in
        t.font = UIFont.systemFont(ofSize: 14)
        t.MaxCharcterCount = 20
        t.attributedPlaceholder = "输入18位身份证号".zm.attributedStr(attrs: [.foregroundColor : "#999999".zm.hex()])
        t.keyboardType = .asciiCapable
        t.delegate = InputMananger.shard.noChinese
    }
    
    // 确认按钮
    let confirmButton = ZMUtil.button(title: "立即认证", font: UIFont.systemFont(ofSize: 16), color: .white).then({$0.backgroundColor = Style.mainColor;$0.layer.cornerRadius = 23})
    //MARK: --------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
    }
    //MARK: --------------------UI--------------------
    func setUI() {
        title = "实名认证"
        view.addSubview(zjlx)
        view.backgroundColor = "#F5F5F5".zm.hex()
        zjlx.frame = CGRect(x: 16, y: 16, width: 0, height: 0)
        zjlx.sizeToFit()
        view.addSubview(typeView)
        typeView.frame = CGRect(x: 16, y: 44, width: SCREEN_W - 32, height: 48)
        
        view.addSubview(xm)
        xm.frame = CGRect(x: 16, y: typeView.zm.bottom + 16, width: SCREEN_W - 32, height: 0)
        xm.sizeToFit()
        
        view.addSubview(nameContent)
        nameContent.frame = CGRect(x: 16, y: xm.zm.bottom + 8, width: SCREEN_W - 32, height: 48)
        
        nameContent.addSubview(nameText)
        nameText.frame = CGRect(x: 12, y: 0, width: nameContent.zm.width, height: 48)
        
        view.addSubview(sfzh)
        sfzh.frame = CGRect(x: 16, y: nameContent.zm.bottom + 16, width: SCREEN_W - 32, height: 0)
        sfzh.sizeToFit()
        
        view.addSubview(idcardContent)
        idcardContent.frame = CGRect(x: 16, y: sfzh.zm.bottom + 8, width: SCREEN_W - 32, height: 48)
        
        idcardContent.addSubview(idcardText)
        idcardText.frame = CGRect(x: 12, y: 0, width: nameContent.zm.width, height: 48)
        
        view.addSubview(confirmButton)
        confirmButton.frame = CGRect(x: 16, y: idcardContent.zm.bottom + 48, width: SCREEN_W - 32, height: 46)

    }
    func addConstraints() {
        
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
        let nameOk = nameText.rx.text.orEmpty.map({$0.count >= 2})
        let idOk = idcardText.rx.text.orEmpty.map({$0.zm.isIDCardNum()})

        
        Observable.combineLatest(nameOk, idOk).map({$0 && $1}).subscribe(onNext:{ [unowned self] resp in
            self.confirmButton.isEnabled = resp
            self.confirmButton.backgroundColor = resp ? Style.mainColor : Style.disableColor
        }).disposed(by: disposeBag)
        
        
        confirmButton.rx.tap.subscribe(onNext:{ [unowned self] resp in
            self.realName()
        }).disposed(by: disposeBag)
    }
    
    func realName() {
        ZMToast.showActivity()
        MineAPI.realName(["idCard" : idcardText.text!, "name" : nameText.text!]).rxRequest(type: Bool.self).subscribe(onNext:{ [unowned self] resp in
            if resp {
                ZMToast.showToast(title: "实名成功")
                ZMAppShareData.share.requestUserInfo { ok in
                    
                }
                self.navigationController?.popViewController(animated: true)
            } else {
                ZMToast.showToast(title: "实名失败")
            }
        }).disposed(by: disposeBag)
    }

    //MARK: --------------------delegate--------------------
}
